using System;
using System.Collections.Generic;
using Microsoft.Win32;


namespace Lapas.Common
{
	/// <summary>
	/// Cung cấp các method đọc, ghi registry
	/// </summary>
	public class WinRegistry
	{
		public WinRegistry()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		/// <summary>
		/// Ghi một key vào registry
		/// </summary>
        /// <param name="key">Key</param>
		/// <param name="subkeydata"> Giá trị của key </param>
		public void Write(string key,string keydata)
		{
			RegistryKey rk = Registry.CurrentUser.OpenSubKey(string.Format("Software\\{0}",AppSettings.ProductCompany), true);
            if (rk == null)
            {
                rk = Registry.CurrentUser.OpenSubKey("Software", true);
                rk = rk.CreateSubKey(AppSettings.ProductCompany);
                rk = Registry.CurrentUser.OpenSubKey(string.Format("Software\\{0}", AppSettings.ProductCompany), true);
            }
            rk.CreateSubKey(AppSettings.ProductName);
            rk = Registry.CurrentUser.OpenSubKey(string.Format("Software\\{0}\\{1}", AppSettings.ProductCompany,AppSettings.ProductName), true);
            rk.SetValue(key,keydata);
		}
        /// <summary>
		/// Đọc một key từ registry
		/// </summary>
		/// <param name="key"> key </param>
        /// <returns> Giá trị tương ứng với key </returns>
		public string Read(string key)
		{
            RegistryKey rk = Registry.CurrentUser.OpenSubKey(string.Format("Software\\{0}\\{1}", AppSettings.ProductCompany,AppSettings.ProductName), false);
            if (rk != null)
            {
                return (string)rk.GetValue(key);
            }
            else
            {
                return string.Empty;
            }
		}

	} // class WinRegistry
} // namespace SystemConfig
